﻿Imports SaveFormatInterface
Imports SaveFormatInterface.Interfaces
Imports System.IO
Friend Class ARDSME
    Implements Interfaces.SaveFormat
    Public Shared ReadOnly Property EmptyIntArray As Long()
        Get
            Dim output As Long() = {}
            Return output
        End Get
    End Property
    Friend Overloads Function ToType(ByVal RAW As Byte(), ByVal EnableCustDesc As Boolean) As Byte()
        'Dim IsWeb As Boolean = True
        'Dim ExtendedDataLines As String() = ExtendedData.Split("vbcrlf")
        'For Each item In ExtendedDataLines
        '    Dim parts As String() = item.Trim.Split("=")
        '    Select Case parts(0).ToLower.Trim
        '        Case "isweb"
        '            IsWeb.
        '    End Select
        'Next
        Dim GameName As String = ""
        Dim SaveName As String = ""
        Dim SaveDescription As String = ""
        If Not IsOnline Then
            'show popup for game and save name
            Dim d As New DUCDialog
            If d.ShowDialog(Filename, "Conversion code by Matt140, modified by evandixon", EnableCustDesc) = Windows.Forms.DialogResult.OK Then
                GameName = d.GameName
                SaveName = d.SaveName
                SaveDescription = d.SaveDescription
            End If
        Else
            If Not String.IsNullOrEmpty(ExtendedData) Then
                Dim parts() As String = ExtendedData.Split("ÿ")
                If parts.Length > 0 Then
                    GameName = parts(0).Replace("_y", "ÿ").Replace("__", "_")
                End If
                If parts.Length > 1 Then
                    SaveName = parts(1).Replace("_y", "ÿ").Replace("__", "_")
                End If
                If parts.Length > 2 Then
                    SaveDescription = parts(2).Replace("_y", "ÿ").Replace("__", "_")
                End If
            End If
        End If
        Dim stream As New IO.MemoryStream
        'Checks for blank game name
        If (GameName = "") Then
            'Set Default Game Name
            GameName = "Converted Saves"
        End If

        'Checks to see if Game Name exceeds 23 characters
        If (GameName.Length > 23) Then
            'Truncate to 23 characters
            GameName = GameName.Substring(0, 23)
        End If

        'Checks for blank save name
        If (SaveName = "") Then
            'Set default save name
            SaveName = "Converted Save"
        End If

        'Check to see if Save Name exceeds 31 characters
        If (SaveName.Length > 31) Then
            'Truncates to 31 characters
            SaveName = SaveName.Substring(0, 31)
        End If

        'Checks for blank description
        If (SaveDescription = "") Then
            'Set Default Save Description
            SaveDescription = "Conversion code by Matt140, modified by evandixon"
        End If

        'Checks to see if Save Description exceeds 70 characters
        If (SaveDescription.Length > 70) Then
            'Truncates to 70 characters
            SaveDescription = SaveDescription.Substring(0, 70)
        End If

        Dim writer As New BinaryWriter(stream)
        writer.Write(0)
        Dim encoding As New System.Text.ASCIIEncoding
        Dim num As Integer = (23 - GameName.Length)
        writer.Write(encoding.GetBytes(GameName.ToCharArray))
        Dim i As Integer = 1
        Do While (i <= num)
            writer.Write(CByte(0))
            i += 1
        Loop
        writer.Write(CByte(161))
        writer.Write(0)
        num = (31 - SaveName.Length)
        writer.Write(encoding.GetBytes(SaveName.ToCharArray))
        i = 1
        Do While (i <= num)
            writer.Write(CByte(0))
            i += 1
        Loop
        writer.Write(System.Convert.ToInt16(0))
        num = (70 - SaveDescription.Length)
        writer.Write(encoding.GetBytes(SaveDescription.ToCharArray))
        i = 1
        Do While (i <= num)
            writer.Write(CByte(0))
            i += 1
        Loop
        For count As Integer = 1 To 27
            writer.Write(CByte(0))
        Next
        writer.Write(CByte(8))
        writer.Write(CByte(192))
        writer.Write(RAW)
        Dim buffer(RAW.Length + 163) As Byte
        stream.Read(buffer, 0, buffer.Length)
        stream.Position = 0
        For count As Integer = 0 To buffer.Length - 1
            buffer(count) = stream.ReadByte
        Next
        'Dim reader As New BinaryReader(stream)
        'Return reader.ReadBytes(RAW.Length + 164)
        Return buffer
        writer.Close()
        ' Return ToType(RAW, "Converted Saves", "Converted Save", "Conversion code by Matt140, modified by evandixon")
    End Function
    Public Overloads Function ToType(ByVal RAW As Byte()) As Byte() Implements Interfaces.SaveFormat.ToType
        Return ToType(RAW, True)
    End Function
    'Public Overloads Function ToType(ByVal RAW As Byte(), ByVal GameName As String, ByVal SaveName As String, ByVal SaveDescription As String) As Byte() Implements SaveFormat.ToType
    '    Dim stream As New IO.MemoryStream
    '    'Checks for blank game name
    '    If (GameName = "") Then
    '        'Set Default Game Name
    '        GameName = "Converted Saves"
    '    End If

    '    'Checks to see if Game Name exceeds 23 characters
    '    If (GameName.Length > 23) Then
    '        'Truncate to 23 characters
    '        GameName = GameName.Substring(0, 23)
    '    End If

    '    'Checks for blank save name
    '    If (SaveName = "") Then
    '        'Set default save name
    '        SaveName = "Converted Save"
    '    End If

    '    'Check to see if Save Name exceeds 31 characters
    '    If (SaveName.Length > 31) Then
    '        'Truncates to 31 characters
    '        SaveName = SaveName.Substring(0, 31)
    '    End If

    '    'Checks for blank description
    '    If (SaveDescription = "") Then
    '        'Set Default Save Description
    '        SaveDescription = "Conversion code by Matt140, modified by evandixon"
    '    End If

    '    'Checks to see if Save Description exceeds 70 characters
    '    If (SaveDescription.Length > 70) Then
    '        'Truncates to 70 characters
    '        SaveDescription = SaveDescription.Substring(0, 70)
    '    End If

    '    Dim writer As New BinaryWriter(stream)
    '    writer.Write(0)
    '    Dim encoding As New System.Text.ASCIIEncoding
    '    Dim num As Integer = (23 - GameName.Length)
    '    writer.Write(encoding.GetBytes(GameName.ToCharArray))
    '    Dim i As Integer = 1
    '    Do While (i <= num)
    '        writer.Write(CByte(0))
    '        i += 1
    '    Loop
    '    writer.Write(CByte(161))
    '    writer.Write(0)
    '    num = (31 - SaveName.Length)
    '    writer.Write(encoding.GetBytes(SaveName.ToCharArray))
    '    i = 1
    '    Do While (i <= num)
    '        writer.Write(CByte(0))
    '        i += 1
    '    Loop
    '    writer.Write(System.Convert.ToInt16(0))
    '    num = (70 - SaveDescription.Length)
    '    writer.Write(encoding.GetBytes(SaveDescription.ToCharArray))
    '    i = 1
    '    Do While (i <= num)
    '        writer.Write(CByte(0))
    '        i += 1
    '    Loop
    '    For count As Integer = 1 To 27
    '        writer.Write(CByte(0))
    '    Next
    '    writer.Write(CByte(8))
    '    writer.Write(CByte(192))
    '    writer.Write(RAW)
    '    Dim buffer(RAW.Length + 163) As Byte
    '    stream.Read(buffer, 0, buffer.Length)
    '    stream.Position = 0
    '    For count As Integer = 0 To buffer.Length - 1
    '        buffer(count) = stream.ReadByte
    '    Next
    '    'Dim reader As New BinaryReader(stream)
    '    'Return reader.ReadBytes(RAW.Length + 164)
    '    Return buffer
    '    writer.Close()
    'End Function
    Public Function ToRAW(ByVal Type As Byte()) As Byte() Implements Interfaces.SaveFormat.ToRaw
        Dim stream As New IO.MemoryStream
        Dim writer As New BinaryWriter(stream)
        For count As Integer = 164 To Type.Length - 1
            writer.Write(Type(count))
        Next
        Dim buffer(Type.Length - 165) As Byte
        stream.Read(buffer, 0, buffer.Length)
        stream.Position = 0
        For count As Integer = 0 To buffer.Length - 1
            buffer(count) = stream.ReadByte
        Next
        Return buffer
    End Function
    Property IsOnline As Boolean Implements Interfaces.SaveFormat.IsOnline
    Property Filename As String Implements Interfaces.SaveFormat.Filename
    Property ExtendedData As String Implements Interfaces.SaveFormat.ExtendedData
    Public ReadOnly Property FileExtension As String Implements Interfaces.SaveFormat.FileExtension
        Get
            Return "duc"
        End Get
    End Property
    Public ReadOnly Property FileSize As Long() Implements Interfaces.SaveFormat.FileSize
        Get
            Return EmptyIntArray
        End Get
    End Property
    Public ReadOnly Property Name As String Implements Interfaces.SaveFormat.Name
        Get
            Return "Action Replay DS Media Edition"
        End Get
    End Property
    Public Overrides Function ToString() As String Implements SaveFormat.ToString
        Return Name
    End Function
    Public ReadOnly Property CanBeSource As Boolean Implements Interfaces.SaveFormat.CanBeSource
        Get
            Return True
        End Get
    End Property
    Public ReadOnly Property CanBeTarget As Boolean Implements Interfaces.SaveFormat.CanBeTarget
        Get
            Return True
        End Get
    End Property

End Class
Friend Class ARDSi
    Implements SaveFormat
    Public Function ToRaw(ByVal Type As Byte()) As Byte() Implements SaveFormat.ToRaw
        Dim a As New ARDSME
        a.Filename = Filename
        a.IsOnline = IsOnline
        a.ExtendedData = ExtendedData
        Return a.ToRAW(Type)
    End Function
    Public Function ToType(ByVal RAW As Byte()) As Byte() Implements SaveFormat.ToType
        Dim a As New ARDSME
        a.Filename = Filename
        a.IsOnline = IsOnline
        a.ExtendedData = ExtendedData
        Return a.ToType(RAW, False)
    End Function
    Property IsOnline As Boolean Implements SaveFormat.IsOnline
    Property Filename As String Implements SaveFormat.Filename
    Property ExtendedData As String Implements SaveFormat.ExtendedData
    Public ReadOnly Property FileExtension As String Implements SaveFormat.FileExtension
        Get
            Return "duc"
        End Get
    End Property
    Public ReadOnly Property FileSize As Long() Implements SaveFormat.FileSize
        Get
            Return ARDSME.EmptyIntArray
        End Get
    End Property
    Public ReadOnly Property Name As String Implements SaveFormat.Name
        Get
            Return "Action Replay DSi"
        End Get
    End Property
    Public ReadOnly Property CanBeSource As Boolean Implements Interfaces.SaveFormat.CanBeSource
        Get
            Return True
        End Get
    End Property
    Public ReadOnly Property CanBeTarget As Boolean Implements Interfaces.SaveFormat.CanBeTarget
        Get
            Return True
        End Get
    End Property
    Public Overrides Function ToString() As String Implements SaveFormat.ToString
        Return Name
    End Function
End Class
Friend Class ARDSMax
    Implements SaveFormat
    Public Function ToRaw(ByVal Type As Byte()) As Byte() Implements SaveFormat.ToRaw
        Dim stream As New IO.MemoryStream
        Dim writer As New BinaryWriter(stream)
        For count As Integer = 500 To Type.Length - 1
            writer.Write(Type(count))
        Next
        Dim buffer(Type.Length - 501) As Byte
        stream.Read(buffer, 0, buffer.Length)
        stream.Position = 0
        For count As Integer = 0 To buffer.Length - 1
            buffer(count) = stream.ReadByte
        Next
        Return buffer
    End Function
    Public Function ToType(ByVal RAW As Byte()) As Byte() Implements SaveFormat.ToType
        Dim GameName As String = ""
        Dim SaveName As String = ""
        Dim SaveDescription As String = ""
        If Not IsOnline Then
            'show popup for game and save name
            Dim d As New DUCDialog
            If d.ShowDialog(Filename, "Conversion code by UniqueGeeks.Net", True) = Windows.Forms.DialogResult.OK Then
                GameName = d.GameName
                SaveName = d.SaveName
                SaveDescription = d.SaveDescription
            End If
        Else
            If Not String.IsNullOrEmpty(ExtendedData) Then
                Dim parts() As String = ExtendedData.Split("ÿ")
                If parts.Length > 0 Then
                    GameName = parts(0).Replace("_y", "ÿ").Replace("__", "_")
                End If
                If parts.Length > 1 Then
                    SaveName = parts(1).Replace("_y", "ÿ").Replace("__", "_")
                End If
                If parts.Length > 2 Then
                    SaveDescription = parts(2).Replace("_y", "ÿ").Replace("__", "_")
                End If
            End If
        End If
        Dim stream As New IO.MemoryStream
        'Checks for blank game name
        If (GameName = "") Then
            'Set Default Game Name
            GameName = "Converted Saves"
        End If

        'Checks to see if Game Name exceeds 23 characters
        If (GameName.Length > 23) Then
            'Truncate to 23 characters
            GameName = GameName.Substring(0, 23)
        End If

        'Checks for blank save name
        If (SaveName = "") Then
            'Set default save name
            SaveName = "Converted Save"
        End If

        'Check to see if Save Name exceeds 31 characters
        If (SaveName.Length > 31) Then
            'Truncates to 31 characters
            SaveName = SaveName.Substring(0, 31)
        End If

        'Checks for blank description
        If (SaveDescription = "") Then
            'Set Default Save Description
            SaveDescription = "Conversion code by UniqueGeeks.Net"
        End If

        'Checks to see if Save Description exceeds 70 characters
        If (SaveDescription.Length > 70) Then
            'Truncates to 70 characters
            SaveDescription = SaveDescription.Substring(0, 70)
        End If

        Dim writer As New BinaryWriter(stream)
        writer.Write(CByte(65))
        writer.Write(CByte(82))
        writer.Write(CByte(68))
        writer.Write(CByte(83))
        writer.Write(CByte(48))
        writer.Write(CByte(48))
        writer.Write(CByte(48))
        writer.Write(CByte(48))
        writer.Write(CByte(48))
        writer.Write(CByte(48))
        writer.Write(CByte(48))
        writer.Write(CByte(48))
        writer.Write(CByte(48))
        writer.Write(CByte(48))
        writer.Write(CByte(48))
        writer.Write(CByte(49))
        Dim encoding As New System.Text.UnicodeEncoding
        Dim num As Integer = (31 - GameName.Length)
        writer.Write(encoding.GetBytes(GameName.ToCharArray))
        Dim i As Integer = 1
        Do While (i <= num)
            writer.Write(CByte(0))
            writer.Write(CByte(0))
            i += 1
        Loop
        writer.Write(CByte(46))
        writer.Write(CByte(0))
        num = (31 - SaveName.Length)
        writer.Write(encoding.GetBytes(SaveName.ToCharArray))
        i = 1
        Do While (i <= num)
            writer.Write(CByte(0))
            writer.Write(CByte(0))
            i += 1
        Loop
        writer.Write(CByte(0))
        writer.Write(CByte(0))
        num = (100 - SaveDescription.Length)
        writer.Write(encoding.GetBytes(SaveDescription.ToCharArray))
        i = 1
        Do While (i <= num)
            writer.Write(CByte(0))
            writer.Write(CByte(0))
            i += 1
        Loop
        'For count As Integer = 1 To 60
        '    writer.Write(CByte(0))
        'Next
        Dim ascii As New System.Text.ASCIIEncoding
        num = (28 - GameName.Length)
        writer.Write(ascii.GetBytes(GameName.ToCharArray))
        i = 1
        Do While (i <= num)
            writer.Write(CByte(0))
            i += 1
        Loop
        writer.Write(CByte(13))
        num = (32 - SaveName.Length)
        writer.Write(ascii.GetBytes(SaveName.ToCharArray))
        i = 1
        Do While (i <= num)
            writer.Write(CByte(0))
            i += 1
        Loop
        'writer.Write(CByte(0))
        num = (95 - SaveDescription.Length)
        writer.Write(ascii.GetBytes(SaveDescription.ToCharArray))
        i = 1
        Do While (i <= num)
            writer.Write(CByte(0))
            i += 1
        Loop
        'For count As Integer = 1 To 27
        '    writer.Write(CByte(0))
        'Next
        'writer.Write(CByte(8))
        'writer.Write(CByte(192))
        writer.Write(RAW)
        Dim buffer(RAW.Length + 499) As Byte
        stream.Read(buffer, 0, buffer.Length)
        stream.Position = 0
        For count As Integer = 0 To buffer.Length - 1
            buffer(count) = stream.ReadByte
        Next
        'Dim reader As New BinaryReader(stream)
        'Return reader.ReadBytes(RAW.Length + 164)
        Return buffer
        writer.Close()
    End Function
    Property IsOnline As Boolean Implements SaveFormat.IsOnline
    Property Filename As String Implements SaveFormat.Filename
    Property ExtendedData As String Implements SaveFormat.ExtendedData
    Public ReadOnly Property FileExtension As String Implements SaveFormat.FileExtension
        Get
            Return "duc"
        End Get
    End Property
    Public ReadOnly Property FileSize As Long() Implements SaveFormat.FileSize
        Get
            Return ARDSME.EmptyIntArray
        End Get
    End Property
    Public ReadOnly Property Name As String Implements SaveFormat.Name
        Get
            Return "Action Replay DS Max"
        End Get
    End Property
    Public ReadOnly Property CanBeSource As Boolean Implements Interfaces.SaveFormat.CanBeSource
        Get
            Return True
        End Get
    End Property
    Public ReadOnly Property CanBeTarget As Boolean Implements Interfaces.SaveFormat.CanBeTarget
        Get
            Return True
        End Get
    End Property
    Public Overrides Function ToString() As String Implements SaveFormat.ToString
        Return Name
    End Function
End Class
Public Class Formats
    Implements SaveFormatCollection
    Public ReadOnly Property SaveFormats As Generic.List(Of SaveFormat) Implements SaveFormatCollection.SaveFormats
        Get
            Dim output As New List(Of SaveFormat)
            output.Add(New ARDSME)
            output.Add(New ARDSi)
            output.Add(New ARDSMax)
            Return output
        End Get
    End Property
End Class
